﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Linq;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

namespace YunPian.Tools
{
    public class WebHelper
    {

        public static string HttpGet(string Url)
        {


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String res = reader.ReadToEnd();
            reader.Close();
            return res;
            //return "true"   
        }
        public static string HttpPost(string Url, string postDataStr)
        {
            byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
            // Console.Write(Encoding.UTF8.GetString(dataArray))
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = dataArray.Length;
            //request.CookieContainer = cookie;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String res = reader.ReadToEnd();
            reader.Close();
            return res;
            //return "true"         
        }

        public static string HttpPostJson(string Url, string postDataStr)
        {
            byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
            // Console.Write(Encoding.UTF8.GetString(dataArray))
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = dataArray.Length;
            //request.CookieContainer = cookie;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String res = reader.ReadToEnd();
            reader.Close();
            return res.ToString();
            //return "true"         
        }


        public static string BuildQuery<T>(T t) 
        {

            var properties = t.GetType().GetProperties();

            var dic = new Dictionary<string, string>();

            foreach (var o in properties) 
            {
                if (o.GetValue(t, null) != null)
                {
                    var temp = o.Name;

                    var attribute = o.GetCustomAttributes(false);

                    if (attribute!=null&&attribute.Count() > 0)
                    {
                        var pro = ((JsonPropertyAttribute)(attribute[0]));

                        temp = pro.PropertyName;

                    }
                    dic.Add(temp, o.GetValue(t, null).ToString());
                }
            }

            dic = dic.OrderBy(o => o.Key).ToDictionary(o => o.Key,o=>o.Value);


            var r = "";
            foreach (var o in dic) 
            {
                r += $"{o.Key}={o.Value}&";            
            }

            if (r.Length > 0) 
            {
                r = r.Substring(0,r.Length-1);
            
            }
            return r;

        
        
        }

    }
}
